package com.then.architecture.di;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import com.then.architecture.BuildConfigCallback;
import com.then.architecture.InjectorMap;

import javax.inject.Inject;

import dagger.android.support.AndroidSupportInjection;
import dagger.android.support.HasSupportFragmentInjector;
import timber.log.Timber;

/**
 * Created by 42524 on 2018/1/12.
 */

public class DaggerFragmentLifecycleCallbacks extends FragmentManager.FragmentLifecycleCallbacks {


    private InjectorMap injectorMap;

    @Inject
    public DaggerFragmentLifecycleCallbacks(InjectorMap injectorMap) {
        this.injectorMap = injectorMap;
    }

    @Override
    public void onFragmentAttached(FragmentManager fm, Fragment f, Context context) {
        super.onFragmentAttached(fm, f, context);
        Timber.i(f.toString() + " ---> onFragmentAttached");
        if (f instanceof BuildConfigCallback) {
            BuildConfigCallback buildConfigCallback = (BuildConfigCallback) f;
            String buildConfig = buildConfigCallback.buildConfigId();
            HasSupportFragmentInjector supportFragmentInjector = injectorMap.getSupportFragmentInjector(buildConfig);
            supportFragmentInjector.supportFragmentInjector().inject(f);
        }
    }

    @Override
    public void onFragmentActivityCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) {
        super.onFragmentActivityCreated(fm, f, savedInstanceState);
        Timber.i(f.toString() + " ---> onFragmentActivityCreated");
    }

    @Override
    public void onFragmentDetached(FragmentManager fm, Fragment f) {
        super.onFragmentDetached(fm, f);
        Timber.i(f.toString() + " ---> onFragmentDetached");
    }

}
